package com.kizuki.proxy;

import org.junit.Test;
import org.springframework.cglib.proxy.Enhancer;

public class Main {

    /**
     * JDKProxy
     */
    @Test
    public void testJDKProxy() {
        IuserDao userDao = new UserDao();
        IuserDao proxy = (IuserDao)new JDKProxyFactory(userDao).newUserDaoProxy();

        System.out.println("userDao.class = " + userDao.getClass());
        System.out.println("proxy.class = " + proxy.getClass());

        proxy.test();

        /**
         * 开始代理
         * 结束代理准备返回
         * 1023487453
         */
        System.out.println(proxy.hashCode());
        System.out.println(proxy.toString());
    }

    /**
     * CglibProxy
     */
    @Test
    public void testCglib() {
        CglibProxyObject proxy = (CglibProxyObject) newCglibProxyInstance();
        proxy.test();
        proxy.hashCode();
    }


    private static Object newCglibProxyInstance() {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(CglibProxyObject.class);

        enhancer.setCallback(new MyMethodInterceptor());
        return enhancer.create();

    }

    /**
     * 静态代理 如Thread
     */

}
